package org.example.test;

import java.util.Stack;

public class Solution02 {
    public static void main(String[] args) {
        Solution02 sol = new Solution02();
        String str="1#2x3#4x5";
        System.out.println(sol.calculate(str));

    }

    public int calculate (String expr) {
        // write code here
        char[] chars = expr.toCharArray();
        Stack<Character> stack = new Stack<>();
        for(char c : chars) {
            if(!stack.isEmpty()&&stack.peek()=='#') {
                stack.pop();
                int add1=Integer.parseInt(stack.pop().toString());
                stack.push((char) (add1+(int)c));
            }else{
                stack.push(c);
            }
        }
        int res=Integer.parseInt(stack.pop().toString());
        while(!stack.isEmpty()){
            if(stack.peek()=='x') {
                stack.pop();
            }else{
                res*=Integer.parseInt(stack.pop().toString());
            }
        }
        return res;
    }
}
